package com.drinks.yiniang.ui.activity.user.login;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.user.LoginJson;
import com.drinks.yiniang.io.api.LoginApiIO;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.drinks.yiniang.ui.activity.MainActivity;
import com.drinks.yiniang.ui.widget.edittext.CodeEditText;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.drinks.yiniang.utils.SharedPreUtils;
import com.hansen.library.utils.StringUtils;

public class VerificationCodeActivity extends BaseTranBarActivity {

    //返回
    private ImageView iv_code_back;
    //发送到手机提示
    private TextView tv_code_to_phone_tips;
    //验证码
    private CodeEditText et_verification_code;
    //确定
    private Button btn_code_sure;

    //登录手机号
    private String mMobile;

    @Override
    protected void initView() {
        iv_code_back = findViewById(R.id.iv_code_back);

        tv_code_to_phone_tips = findViewById(R.id.tv_code_to_phone_tips);

        et_verification_code = findViewById(R.id.et_verification_code);

        btn_code_sure = findViewById(R.id.btn_code_sure);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        mMobile = getIntent().getStringExtra(Constants.KeyMobile);

        SpannableStringBuilder tips = new SpannableStringBuilder(String.format(getString(R.string.text_send_code_to_phone), ""));
        int start = tips.length();
        tips.append("+86 ");
        tips.append(StringUtils.getNullEmptyConvert__(mMobile));

        tips.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.color_b4b4b4)), start, tips.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        tv_code_to_phone_tips.setText(tips);
    }

    @Override
    protected void initListener() {
        iv_code_back.setOnClickListener(this);
        btn_code_sure.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.iv_code_back://返回
                finish();
                break;
            case R.id.btn_code_sure://确认
                doLogin();
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_verification_code;
    }

    private void doLogin() {
        if (StringUtils.isEmpty(mMobile)) {
            ToastUtils.showShort("手机号为空, 请返回重试!");
            return;
        }

        String code = StringUtils.getEditTextString(et_verification_code.getEditableText());

        if (StringUtils.isEmpty(code) || code.length() < 6) {
            ToastUtils.showShort(getString(R.string.text_hint_input_verification_code));
            return;
        }

        showLoadingDialog();

        LoginApiIO.getInstance().doUserLogin(mMobile, code, new APIRequestCallback<LoginJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(LoginJson res) {
                SharedPreUtils.getInstance().saveUserToken(res.getData().getAccess_token());

                if(res.getData().getUser() != null) {
                    SharedPreUtils.getInstance().saveUserID(res.getData().getUser().getId());
                    SharedPreUtils.getInstance().saveIsOpenPayPasswordID(Constants.STATUS_GLOBAL_YES.equals(res.getData().getUser().getOpen_pay()) ? Constants.STATUS_GLOBAL_YES : Constants.STATUS_GLOBAL_NO);
                }

                Intent intent = new Intent(mContext, MainActivity.class);
                startActivity(intent);

                setResult(Constants.RESULT_CODE_NORMAL);
                finish();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
